// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/encode/base64.h>

#include <gtest/gtest.h>
#include <pollux/common/base/exceptions.h>
#include <pollux/testing/gtest_utils.h>

namespace kumo::pollux::encoding {

class Base64Test : public ::testing::Test {};

TEST_F(Base64Test, fromBase64) {
  EXPECT_EQ(
      "Hello, World!",
      Base64::decode(melon::StringPiece("SGVsbG8sIFdvcmxkIQ==")));
  EXPECT_EQ(
      "Base64 encoding is fun.",
      Base64::decode(melon::StringPiece("QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4=")));
  EXPECT_EQ(
      "Simple text", Base64::decode(melon::StringPiece("U2ltcGxlIHRleHQ=")));
  EXPECT_EQ(
      "1234567890", Base64::decode(melon::StringPiece("MTIzNDU2Nzg5MA==")));

  // Check encoded strings without padding
  EXPECT_EQ(
      "Hello, World!",
      Base64::decode(melon::StringPiece("SGVsbG8sIFdvcmxkIQ")));
  EXPECT_EQ(
      "Base64 encoding is fun.",
      Base64::decode(melon::StringPiece("QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4")));
  EXPECT_EQ(
      "Simple text", Base64::decode(melon::StringPiece("U2ltcGxlIHRleHQ")));
  EXPECT_EQ("1234567890", Base64::decode(melon::StringPiece("MTIzNDU2Nzg5MA")));
}

TEST_F(Base64Test, calculateDecodedSizeProperSize) {
  size_t encodedSize = 20;
  EXPECT_EQ(
      13,
      Base64::calculateDecodedSize("SGVsbG8sIFdvcmxkIQ==", encodedSize)
          .value());
  EXPECT_EQ(18, encodedSize);

  encodedSize = 18;
  EXPECT_EQ(
      13,
      Base64::calculateDecodedSize("SGVsbG8sIFdvcmxkIQ", encodedSize).value());
  EXPECT_EQ(18, encodedSize);

  encodedSize = 21;
  EXPECT_EQ(
      Status::UserError(
          "Base64::decode() - invalid input string: string length is not a multiple of 4."),
      Base64::calculateDecodedSize("SGVsbG8sIFdvcmxkIQ===", encodedSize)
          .error());

  encodedSize = 32;
  EXPECT_EQ(
      23,
      Base64::calculateDecodedSize(
          "QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4=", encodedSize)
          .value());
  EXPECT_EQ(31, encodedSize);

  encodedSize = 31;
  EXPECT_EQ(
      23,
      Base64::calculateDecodedSize(
          "QmFzZTY0IGVuY29kaW5nIGlzIGZ1bi4", encodedSize)
          .value());
  EXPECT_EQ(31, encodedSize);

  encodedSize = 16;
  EXPECT_EQ(
      10,
      Base64::calculateDecodedSize("MTIzNDU2Nzg5MA==", encodedSize).value());
  EXPECT_EQ(14, encodedSize);

  encodedSize = 14;
  EXPECT_EQ(
      10, Base64::calculateDecodedSize("MTIzNDU2Nzg5MA", encodedSize).value());
  EXPECT_EQ(14, encodedSize);
}

TEST_F(Base64Test, checksPadding) {
  EXPECT_TRUE(Base64::isPadded("ABC=", 4));
  EXPECT_FALSE(Base64::isPadded("ABC", 3));
}

TEST_F(Base64Test, countsPaddingCorrectly) {
  EXPECT_EQ(0, Base64::numPadding("ABC", 3));
  EXPECT_EQ(1, Base64::numPadding("ABC=", 4));
  EXPECT_EQ(2, Base64::numPadding("AB==", 4));
}
} // namespace kumo::pollux::encoding
